import com.alibaba.fastjson.JSON;
import com.token.base.FuncUtil;
import com.token.wsset.soap.SendSoap;
import com.token.wsset.soap.SoapXML;
import com.token.wsset.soapxml.*;
import lotus.domino.AgentBase;

import java.io.PrintWriter;
import java.lang.reflect.Method;

public class JavaAgent extends AgentBase {

    FuncUtil f;
    boolean isJsonp;
    public void NotesMain() {

        try {
            f = new FuncUtil(getSession());
            String callback = f.getUrlQueryString("jsonpCallback");
            String url = f.getUrlQueryString("url");
            if(url == null || "".equals(url)){
                url= "http://192.168.101.110:80/dev/Sys_WebService.nsf/FlowAction?OpenWebService";
            }

            String typeName = f.getUrlQueryString("ApiType");
            Class<?> c = this.getClass();
            Method method = c.getMethod(typeName);
            SoapXML body = (SoapXML) method.invoke(this);

            SendSoap send = new SendSoap();
            if(send.httpPost(url,body)){
                f.setJsonData(JSON.parseObject(send.getReturnJsonString()));
            }else{
                f.putJsonData("code", send.getErrorCode());
                f.putJsonData("message", send.getErrorDetail());
            }

            isJsonp = callback != null && !"".equals(callback);

            if(isJsonp){
                PrintWriter out = getAgentOutput();
                out.println("Content-type:text/html;charset=utf-8");
                out.println("showData("+ f.getJsonData() +")");//返回jsonp格式数据
                out.flush();
                out.close();
            }else{
                f.printJsonData(getAgentOutput());
            }

        } catch(Exception e) {
            if(isJsonp){
                PrintWriter out = getAgentOutput();
                out.println("Content-type:text/html;charset=utf-8");
                out.println("showData('"+ e.toString() +"')");//返回jsonp格式数据
                out.flush();
                out.close();
            }else{
                f.printWriteErr(getAgentOutput(), e);
            }
            e.printStackTrace();
        }finally{
            f.recycle();
        }
    }

    public SoapXML SubmitFlowSoap(){
        SubmitFlowSoap soap = new SubmitFlowSoap();
        soap.setActionUser(f.getUrlQueryString("actionUser"));
        soap.setKeyUnid(f.getUrlQueryString("keyUnid"));
        soap.setApplyer(f.getUrlQueryString("applyer"));
        soap.setAppKeyId(f.getUrlQueryString("appKeyId"));

        soap.setSelectApprovers(f.getUrlQueryString("selectApprovers"));
        soap.setFlowId(f.getUrlQueryString("flowId"));
        soap.setStepId(f.getUrlQueryString("stepId"));
        soap.setCreater(f.getUrlQueryString("creater"));
        soap.setSubKeyUnid(f.getUrlQueryString("subKeyUnid")); //子流程KeyUnid
        soap.setOpinions(f.getUrlQueryString("opinions"));
        soap.setResult(f.getUrlQueryString("result")); //会审使用

        String fv = f.getUrlQueryString("fieldValues");
        if(fv != null && !"".equals(fv)){
            soap.setFieldValues(JSON.parseObject(fv));
        }
        return soap;
    }

    public SoapXML GetPathApproverSoap(){
        GetPathApproverSoap soap = new GetPathApproverSoap();
        soap.setActionUser(f.getUrlQueryString("actionUser"));
        soap.setKeyUnid(f.getUrlQueryString("keyUnid"));
        soap.setApplyer(f.getUrlQueryString("applyer"));
        soap.setFlowId(f.getUrlQueryString("flowId"));
        soap.setStepId(f.getUrlQueryString("stepId"));
        soap.setCreater(f.getUrlQueryString("creater"));

        String fv = f.getUrlQueryString("fieldValues");
        if(fv != null && !"".equals(fv)){
            soap.setFieldValues(JSON.parseObject(fv));
        }
        return soap;
    }

    public SoapXML LoadFlowStepSoap(){
        LoadFlowStepSoap soap = new LoadFlowStepSoap();
        soap.setAppKeyId(f.getUrlQueryString("appKeyId"));
        soap.setActionUser(f.getUrlQueryString("actionUser"));
        soap.setKeyUnid(f.getUrlQueryString("keyUnid"));
        soap.setApplyer(f.getUrlQueryString("applyer"));
        return soap;
    }


    public SoapXML GetReturnStepSoap() throws Exception {
        GetReturnStepSoap soap = new GetReturnStepSoap();
        soap.setActionUser(f.getUrlQueryString("actionUser"));
        soap.setKeyUnid(f.getUrlQueryString("keyUnid"));
        return soap;
    }

    public SoapXML ReturnFlowSoap() throws Exception {
        ReturnFlowSoap soap = new ReturnFlowSoap();
        soap.setFlowId(f.getUrlQueryString("flowId"));
        soap.setStepId(f.getUrlQueryString("stepId"));
        soap.setSelectApprovers(f.getUrlQueryString("selectApprovers"));
        soap.setOpinions(f.getUrlQueryString("opinions"));
        return soap;
    }

    public SoapXML BackFlowSoap() throws Exception {
        BackFlowSoap soap = new BackFlowSoap();
        soap.setActionUser(f.getUrlQueryString("actionUser"));
        soap.setKeyUnid(f.getUrlQueryString("keyUnid"));
        soap.setOpinions(f.getUrlQueryString("opinions"));
        return soap;
    }

    public SoapXML OverFlowSoap() throws Exception {
        OverFlowSoap soap = new OverFlowSoap();
        soap.setActionUser(f.getUrlQueryString("actionUser"));
        soap.setKeyUnid(f.getUrlQueryString("keyUnid"));
        soap.setOpinions(f.getUrlQueryString("opinions"));
        return soap;
    }

    public SoapXML SysSetFlowSoap() throws Exception {
        SysSetFlowSoap soap = new SysSetFlowSoap();
        soap.setActionUser(f.getUrlQueryString("actionUser"));
        soap.setKeyUnid(f.getUrlQueryString("keyUnid"));
        soap.setFlowId(f.getUrlQueryString("flowId"));
        soap.setStepId(f.getUrlQueryString("stepId"));
        soap.setSelectApprovers(f.getUrlQueryString("selectApprovers"));
        soap.setOpinions(f.getUrlQueryString("opinions"));
        return soap;
    }

    public SoapXML TransmitFlowSoap() throws Exception {
        TransmitFlowSoap soap = new TransmitFlowSoap();
        soap.setActionUser(f.getUrlQueryString("actionUser"));
        soap.setKeyUnid(f.getUrlQueryString("keyUnid"));
        soap.setSelectApprovers(f.getUrlQueryString("selectApprovers"));
        soap.setOpinions(f.getUrlQueryString("opinions"));
        return soap;
    }

    public SoapXML CreateCallHQSoap() throws Exception {
        CreateCallHQSoap soap = new CreateCallHQSoap();
        soap.setActionUser(f.getUrlQueryString("actionUser"));
        soap.setKeyUnid(f.getUrlQueryString("keyUnid"));
        soap.setSelectApprovers(f.getUrlQueryString("selectApprovers"));
        soap.setOpinions(f.getUrlQueryString("opinions"));
        return soap;
    }

    public SoapXML CreateCallHSSoap() throws Exception {
        CreateCallHSSoap soap = new CreateCallHSSoap();
        soap.setActionUser(f.getUrlQueryString("actionUser"));
        soap.setKeyUnid(f.getUrlQueryString("keyUnid"));
        soap.setSelectApprovers(f.getUrlQueryString("selectApprovers"));
        soap.setOpinions(f.getUrlQueryString("opinions"));
        soap.setHsway(f.getUrlQueryString("hsway"));
        return soap;
    }

    public SoapXML CCFlowSoap() throws Exception {
        CCFlowSoap soap = new CCFlowSoap();
        soap.setActionUser(f.getUrlQueryString("actionUser"));
        soap.setKeyUnid(f.getUrlQueryString("keyUnid"));
        soap.setSelectApprovers(f.getUrlQueryString("selectApprovers"));
        soap.setOpinions(f.getUrlQueryString("opinions"));
        return soap;
    }

    public SoapXML UrgFlowSoap() throws Exception {
        UrgFlowSoap soap = new UrgFlowSoap();
        soap.setActionUser(f.getUrlQueryString("actionUser"));
        soap.setKeyUnid(f.getUrlQueryString("keyUnid"));
        soap.setOpinions(f.getUrlQueryString("opinions"));
        return soap;
    }

    public SoapXML GetApproveProcessSoap() throws Exception {
        GetApproveProcessSoap soap = new GetApproveProcessSoap();
        soap.setActionUser(f.getUrlQueryString("actionUser"));
        soap.setKeyUnid(f.getUrlQueryString("keyUnid"));
        return soap;
    }

    public SoapXML GetOpinionsSoap() throws Exception {
        GetOpinionsSoap soap = new GetOpinionsSoap();
        soap.setActionUser(f.getUrlQueryString("actionUser"));
        soap.setKeyUnid(f.getUrlQueryString("keyUnid"));
        return soap;
    }
    public SoapXML GetFlowApproveLogSoap() throws Exception {
        GetFlowApproveLogSoap soap = new GetFlowApproveLogSoap();
        soap.setActionUser(f.getUrlQueryString("actionUser"));
        soap.setKeyUnid(f.getUrlQueryString("keyUnid"));
        return soap;
    }

}